package org.niu.listnode;

import java.util.List;

public class Merge {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode ans = new ListNode(-1),p=ans;
        if(list1 == null) return list2;
        else if(list2 == null) return list1;
        ListNode a = list1;
        ListNode b = list2;
        while (b.next != null || a.next!=null) {
            if (a.val <= b.val) {
                p.next = a;
                a = a.next;
                p = p.next;
            } else if(a.val > b.val){
                p.next = b;
                b = b.next;
                p = p.next;
            }
        }
        if(a.next!=null) p.next = a;
        else p.next = b;
        return ans;
    }
}
